package operations;

import MyThrowable.IDRepeatWrongful;
import MyThrowable.InputWrongful;
import student.Student;
import student.StudentList;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:修改学生
 * User: 86183
 * Date: 2022-12-14
 * Time: 9:42
 */
public class Correction implements IOperation{
    static String PatternStr1 = "^[\\u4e00-\\u9fa5]{2,10}$";
    static String PatternStr2 = "^[0-9]+$";
    @Override
    public void work(StudentList studentList) {
        Scanner scan = new Scanner(System.in);
        System.out.println("修改学生信息:");
        System.out.println("显示所有学生:");
        studentList.display();
        System.out.println("请输入要修改学生的学号:");
        int pos = -1;
        while (true){
            int id = scan.nextInt();
            pos = studentList.findIDpos(id);
            if(pos == - 1){
                System.out.println("不存在该学生,请重新输入!");
            }else {
                break;
            }
        }
        Student student = studentList.getPos(pos);
        System.out.println(student);
        while (true){
           int choice = menu();
           int flag = correctoin(student,choice,studentList);
//            if(flag>= 6 || flag < 0){
//                System.out.println("输入错误!");
//            }
            if(flag == 0){
                break;
            }
        }

    }
    public  int menu(){
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("*******************************");
            System.out.println("*****请选择要修改的内容(1~6):******");
            System.out.println("*********** 1.学号 *************");
            System.out.println("*********** 2.姓名 *************");
            System.out.println("*********** 3.专业 *************");
            System.out.println("*********** 4.年龄 *************");
            System.out.println("*********** 5.性别 *************");
            System.out.println("******** 6.返回上一级 ***********");
            System.out.println("*******************************");
            String choice = scan.nextLine();
            if (Pattern.matches(PatternStr2, choice)) {
                return Integer.valueOf(choice);
            }else {
                System.out.println("输入有误!");
            }
        }

    }
    public int correctoin(Student student,int choice,StudentList studentList){
        Scanner scan = new Scanner(System.in);
        switch (choice){
            case 1:
                System.out.println("请输入更改后的学号:(只能输入0~9的数字)");
              while (true) {
                  String newId = scan.nextLine();
                  try {
                      if (!Pattern.matches(PatternStr2, newId)) {
                          throw new InputWrongful("输入有误,请再次输入!");
                      } else if (studentList.isIDReapt(newId)) {
                          throw new IDRepeatWrongful("ID重复,请重新输入!");
                      }
                      student.setId(Integer.valueOf(newId));
                      System.out.println("更改成功");
                      break;
                  } catch (InputWrongful e) {
                      e.printStackTrace();
                  } catch (IDRepeatWrongful e) {
                      e.printStackTrace();
                  }

              }
                System.out.println(student);
                System.out.println("请选择是否继续更改:(Y/N)");
//                scan.nextLine();
                String ch1 = scan.nextLine();
                if(ch1.equals("Y")){
                    return 1;
                }else {
                    break;
                }
            case 2 :
                System.out.println("请输入更改后的姓名(限制汉字个数2~10):");
                while (true){
                    String name =scan.nextLine();
                    try {
                        if (!Pattern.matches(PatternStr1, name)) {
                            throw new InputWrongful("输入有误,请再次输入!");
                        }else {
                            student.setName(name);
                            System.out.println("更改成功");
                            break;
                        }
                    } catch (InputWrongful e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(student);
                System.out.println("请选择是否继续更改:(Y/N)");
                String ch2 = scan.nextLine();
                if(ch2.equals("Y")){
                    return 1;
                }else {
                    break;
                }
            case 3:
                System.out.println("请输入更改后的专业(限制汉字个数2~10):");
                while (true){
                    String ms = scan.nextLine();
                    try {
                        if (!Pattern.matches(PatternStr1, ms)) {
                            throw new InputWrongful("输入有误,请再次输入!");
                        }else {
                            student.setMs(ms);
                            System.out.println("更改成功");
                            break;
                        }
                    } catch (InputWrongful e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(student);
                System.out.println("请选择是否继续更改:(Y/N)");
                String ch3 = scan.nextLine();
                if(ch3.equals("Y")){
                    return 1;
                }else {
                    break;
                }
            case 4:
                System.out.println("请输入更改后的年龄(17~125):");
//
                while (true){
                    int age = scan.nextInt();
                    try {
                        if (age < 17 || age > 125) {
                            throw new InputWrongful("输入有误,请再次输入!");
                        }else {
                            student.setAge(age);
                            System.out.println("更改成功");
                            break;
                        }
                    } catch (InputWrongful e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("请选择是否继续更改:(Y/N)");
                scan.nextLine();
                String ch4 = scan.nextLine();
                if(ch4.equals("Y")){
                    return 1;
                }else {
                    break;
                }
            case 5:
                System.out.println("请输入更改后的性别(F/M):");
//                student.setName(scan.nextLine());
                while (true) {
                    String gender = scan.nextLine();
                    try {
                        if ((!gender.equals("F")) && (!gender.equals("M"))) {
                            throw new InputWrongful("输入有误,请再次输入!");
                        }else {
                            student.setName(gender);
                            System.out.println("更改成功");
                            break;
                        }
                    } catch (InputWrongful e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(student);
                System.out.println("请选择是否继续更改:(Y/N)");
                String ch5 = scan.nextLine();
                if(ch5.equals("Y")){
                    return 1;
                }else {
                    break;
                }
            case 6:
                break;
            default:
//                scan.nextLine();
                System.out.println("输入错误!");
                System.out.println("请选择是否继续更改:(Y/N)");
                String ch = scan.nextLine();
                if(ch.equals("Y")){
                    return 1;
                }else {
                    break;
                }

        }
        return 0;
    }
}
